package net.sunxiaolong.tool.captureinsureknowledge.controller;

import net.sunxiaolong.tool.captureinsureknowledge.entity.GpicKnowledge;
import net.sunxiaolong.tool.captureinsureknowledge.repository.GpicKnowledgeRepository;
import net.sunxiaolong.tool.captureinsureknowledge.tool.capture.GpicCapture;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.XPatherException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 晓龙
 * @create 2018-08-14 上午11:10
 * @desc
 **/
@Controller
@RequestMapping(value = "gpic")
public class GpicCaptureController {

    @Autowired
    private GpicKnowledgeRepository gpicKnowledgeRepository;

    private HtmlCleaner hc = new HtmlCleaner();

    @RequestMapping(value = "gpicCapture")
    @ResponseBody
    public String gpicCapture(String pageUrlStr) throws XPatherException {
        GpicCapture.capture(pageUrlStr);
        return "OK";
    }

    @RequestMapping(value = "exportExcelGpic")
    @ResponseBody
    public String exportExcelSingleThreadGpic() throws IOException {
        System.out.println("startTime:" + LocalDateTime.now().toLocalTime());
        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet cpicSheet = workbook.createSheet("GPIC知识库");
        int thisSheetRowIndex = 0;
        Row headRow = cpicSheet.createRow(thisSheetRowIndex++);
        int thisRowCellIndex = 0;
        headRow.createCell(thisRowCellIndex++).setCellValue("DBID");
        headRow.createCell(thisRowCellIndex++).setCellValue("标题");
        headRow.createCell(thisRowCellIndex++).setCellValue("内容");
        headRow.createCell(thisRowCellIndex++).setCellValue("链接");
        List<GpicKnowledge> knowledgeList = gpicKnowledgeRepository.getAllData();
        int size = knowledgeList.size();
        for (int i = 0; i < size; i++) {
            GpicKnowledge knowledge = knowledgeList.get(i);
            Row thisRow = cpicSheet.createRow(i + 1);
            thisRowCellIndex = 0;
            thisRow.createCell(thisRowCellIndex++).setCellValue(knowledge.getId());
            thisRow.createCell(thisRowCellIndex++).setCellValue(knowledge.getTitle());
            thisRow.createCell(thisRowCellIndex++).setCellValue(knowledge.getContent());
            thisRow.createCell(thisRowCellIndex++).setCellValue(knowledge.getUrl());
        }

        FileOutputStream fos = new FileOutputStream("/tmp/GPIC保险知识库" + System.currentTimeMillis() + ".xlsx");
        workbook.write(fos);
        fos.flush();
        workbook.close();
        fos.close();

        System.out.println("endTime:" + LocalDateTime.now().toLocalTime());
        return "single thread export finish";
    }

}
